
# include "dfxVertexFormat.h"

namespace DFX
{
    VertexElement::VertexElement(const String &name, UInt8 index, UInt16 offset, VERTEX_ELEMENT_FORMAT format, VERTEX_ELEMENT_USAGE usage)
        : mName(name), mIndex(index), mOffset(offset), mFormat(format), mUsage(usage)
    {
    }

    UInt32 VertexElement::GetSize() const
    {
        switch (mFormat)
        {
        case VERTEX_ELEMENT_FORMAT_FLOAT1:
            return 4;

        case VERTEX_ELEMENT_FORMAT_FLOAT2:
            return 8;

        case VERTEX_ELEMENT_FORMAT_FLOAT3:
            return 12;

        case VERTEX_ELEMENT_FORMAT_FLOAT4:
            return 16;

        case VERTEX_ELEMENT_FORMAT_HALF_FLOAT2:
            return 4;

        case VERTEX_ELEMENT_FORMAT_HALF_FLOAT4:
            return 8;

        case VERTEX_ELEMENT_FORMAT_RGBA64:
            return 8;

        case VERTEX_ELEMENT_FORMAT_COLOR:
            return 4;

        case VERTEX_ELEMENT_FORMAT_SHORT2:
            return 4;

        case VERTEX_ELEMENT_FORMAT_SHORT4:
            return 8;
        }

        return 0;
    }    

    VertexElement* VertexFormat::GetElement(UInt32 index)
    {
        if (index < mElements.size())
            return &mElements[index];

        return NULL;
    }

    void VertexFormat::AddElement(const VertexElement &element)
    {
        mElements.push_back(element);
    }
    void VertexFormat::AddElement(const String &name, UInt8 index, UInt16 offset, VERTEX_ELEMENT_FORMAT format, VERTEX_ELEMENT_USAGE usage)
    {
        mElements.push_back(VertexElement(name, index, offset, format, usage));
    }

    void VertexFormat::Update()
    {
        mSize = 0;

        for (UInt32 i = 0; i < mElements.size(); i++)
            mSize += mElements[i].GetSize();

        mNeedUpdate = false;
    }
    void VertexFormat::Clear()
    {
        mSize = 0;
        mElements.clear();
    }
}